﻿using System;

namespace Dogshow.Model.Extensions
{
    public static class StringExtensions
    {
         public static string TrimAndNull(this string str)
         {
             if (str == null) return null;
             var trimmed = str.Trim();
             return trimmed != String.Empty ? trimmed : null;
         } 

        public static string TrimNullAndToUpper(this string str)
        {
            str = str.TrimAndNull();
            if (str != null) return str.ToUpper();
            return null;
        }

        public static string TrimNullAndToSentence(this string str)
        {
            str = str.TrimAndNull();
            if (str != null)
            {
                str = str.ToLower();
                if (str.Length > 1) str = str.Replace(str.Substring(0, 1), str.Substring(0, 1).ToUpper());
                return str;
            }
            return null;
        }

        public static int? ToNullableInt(this string str)
        {
            if (!String.IsNullOrEmpty(str)) return null;
            int ret;
            if (Int32.TryParse(str, out ret))
            {
                return ret;
            }
            return null;
        }
    }
}